/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import engine.linkList.LinkedListManager;
import engine.linkList.LocalToScanFolderNode;
import engine.linkList.MusicListNode;
import engine.linkList.MusicNode;
import engine.linkList.Node;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author hjp
 */
public class ChooseLocalToScanFolderDialog extends javax.swing.JDialog {

    /**
     * Creates new form test
     */
    public ChooseLocalToScanFolderDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        setUndecorated(true);
        initComponents();
//        this.addWindowListener(new java.awt.event.WindowAdapter() {
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        closeButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        addNewFolderButton = new javax.swing.JButton();
        confirmButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        localToScanFolderListPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBackground(new java.awt.Color(250, 250, 250));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(250, 250, 250));
        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.MOVE_CURSOR));
        jPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jPanel1MouseDragged(evt);
            }
        });
        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPanel1MousePressed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("楷体", 0, 24)); // NOI18N
        jLabel1.setText("选择本地音乐文件夹");

        closeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/icons/close.png"))); // NOI18N
        closeButton.setBorder(null);
        closeButton.setBorderPainted(false);
        closeButton.setContentAreaFilled(false);
        closeButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        closeButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                closeButtonMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                closeButtonMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                closeButtonMouseExited(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 119, Short.MAX_VALUE)
                .addComponent(closeButton)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(closeButton)
                    .addComponent(jLabel1))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 400, 70));

        jPanel2.setBackground(new java.awt.Color(250, 250, 250));
        jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 2, 2, 2));

        addNewFolderButton.setBackground(new java.awt.Color(250, 250, 250));
        addNewFolderButton.setFont(new java.awt.Font("楷体", 0, 24)); // NOI18N
        addNewFolderButton.setText("添加文件夹");
        addNewFolderButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        addNewFolderButton.setFocusPainted(false);
        addNewFolderButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                addNewFolderButtonMouseClicked(evt);
            }
        });

        confirmButton.setBackground(new java.awt.Color(250, 250, 250));
        confirmButton.setFont(new java.awt.Font("楷体", 0, 24)); // NOI18N
        confirmButton.setText("确认");
        confirmButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        confirmButton.setFocusPainted(false);
        confirmButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                confirmButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(confirmButton, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24)
                .addComponent(addNewFolderButton, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(addNewFolderButton, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(confirmButton, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 320, 400, 60));

        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        localToScanFolderListPanel.setBackground(new java.awt.Color(250, 250, 250));
        localToScanFolderListPanel.setLayout(new javax.swing.BoxLayout(localToScanFolderListPanel, javax.swing.BoxLayout.Y_AXIS));
        jScrollPane1.setViewportView(localToScanFolderListPanel);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 70, 400, 250));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_closeButtonMouseEntered
        // TODO add your handling code here:
        Icon icon = new ImageIcon(getClass().getResource("/GUI/icons/close_colour.png"));
        closeButton.setIcon(icon);
    }//GEN-LAST:event_closeButtonMouseEntered

    private void closeButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_closeButtonMouseExited
        // TODO add your handling code here:
        Icon icon = new ImageIcon(getClass().getResource("/GUI/icons/close.png"));
        closeButton.setIcon(icon);
    }//GEN-LAST:event_closeButtonMouseExited

    private void closeButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_closeButtonMouseClicked
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_closeButtonMouseClicked

    private void jPanel1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MousePressed
        // TODO add your handling code here:
         locationX = evt.getX();
        locationY = evt.getY();
    }//GEN-LAST:event_jPanel1MousePressed

    private void jPanel1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MouseDragged
        // TODO add your handling code here:
        int left = this.getLocation().x;
        int top = this.getLocation().y;
        this.setLocation(left + evt.getX() - locationX, top + evt.getY() - locationY);
    }//GEN-LAST:event_jPanel1MouseDragged

    private void confirmButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_confirmButtonMouseClicked
        //扫描所有待扫描文件夹
        LinkedListManager.getInstance().setLocalListNode(LinkedListManager.getInstance().scanAndCreateLocalListNode());
        //此时访问的是本地歌单，显然没有歌单集合一说
         LinkedListManager.getInstance().setCurrentVisitMusicLists(null);
         LinkedListManager.getInstance().setCurrentVisitMusicList(LinkedListManager.getInstance().getLocalListNode());
         //设置中间变量
         MusicPlayerUI.getInstance().transientCurrentVisitMusicLists = LinkedListManager.getInstance().getCurrentVisitMusicLists();
         MusicPlayerUI.getInstance().transientCurrentVisitMusicList = LinkedListManager.getInstance().getCurrentVisitMusicList();
        //更新UI
        MusicPlayerUI.getInstance().printMusicList(MusicPlayerUI.getInstance().getMusicListTabel());
        dispose();
    }//GEN-LAST:event_confirmButtonMouseClicked

    private void addNewFolderButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_addNewFolderButtonMouseClicked
        //修正JFileChooser的UI
//             try {
//            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//          } catch (Exception e1) {
//           e1.printStackTrace();
//          }
        //启动文件选择器JFileChooser
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("选择本地音乐文件或文件夹");
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//仅允许选择文件夹
       fileChooser.setMultiSelectionEnabled(true);//允许选择多个文件夹
//       fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
//       FileNameExtensionFilter filterMp3 = new FileNameExtensionFilter("mp3 文件", "mp3");
//       fileChooser.addChoosableFileFilter(filterMp3);
       int returnVal = fileChooser.showOpenDialog(MusicPlayerUI.getInstance());
       //当用户点击确定按钮时才将带扫描文件夹添加到链表
        if (JFileChooser.APPROVE_OPTION == returnVal ) {
            File[] folders = fileChooser.getSelectedFiles();
            if (folders != null) {
                for(File folder : folders){
                    //更新ChooseLocalToScanFolderDialog的UI
                    String path = folder.getAbsolutePath();
                    JCheckBox newCheckBox = new JCheckBox(path);
                    newCheckBox.setFont(new java.awt.Font("楷体", 0, 24));
                    //添加监听器
                    newCheckBox.addMouseListener(new MouseAdapter(){
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            Node<LocalToScanFolderNode> selectedLocalToScanFolderNode = LinkedListManager.getInstance().findSelectedLocalToScanFolderNode(path);
                            selectedLocalToScanFolderNode.info.isSelect = newCheckBox.isSelected();
                        }
                    });
                    //新创建的带扫描的文件夹结点当然默认是已选中的
                    newCheckBox.setSelected(true);
                    localToScanFolderListPanel.add(newCheckBox);
                    localToScanFolderListPanel.revalidate();
                    //将本地带扫描文件夹结点添加到链表管理器中
                    LinkedListManager.getInstance().addLocalToScanFolderNode(path);
                }
            }
        }
    }//GEN-LAST:event_addNewFolderButtonMouseClicked

    public javax.swing.JPanel getLocalToScanFolderListPanel(){
        return localToScanFolderListPanel;
    }
    
    private int locationX;
    private int locationY;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addNewFolderButton;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton closeButton;
    private javax.swing.JButton confirmButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel localToScanFolderListPanel;
    // End of variables declaration//GEN-END:variables
}
